import { loadCss, loadModules } from 'esri-loader'
import arcgisConfig from "../../url/argics.js";
import clickView from "./click.js";
//外部组件引用
// window.dojoConfig = {
//   parseOnLoad: true,
//   async: true,
//   packages: [{
//     name: 'integration',
//     location: '/src/modules/qls_web/assets/integration'
//   }]
// }
export default function createMap(args) {
    return new Promise(function(resolve, reject) {
        const options = {
            url: arcgisConfig.getArgicsJs
        };
        loadCss(arcgisConfig.getArgicsCss)
        loadModules([
            //配置部分
            "esri/config",
            //地图对象部分
            "esri/Basemap",
            "esri/Map",
            "esri/views/MapView",
            "esri/views/SceneView",
            //图层部分
            "esri/layers/TileLayer",
            "esri/layers/GeoJSONLayer",
            "esri/layers/MapImageLayer",
            "esri/layers/FeatureLayer",
            "esri/layers/GraphicsLayer",
            "esri/ztelit/ClusterLayer",
            "esri/ztelit/ArrowLine",
            "esri/layers/WebTileLayer",
            //几何对象部分
            'esri/geometry/Point',
            "esri/geometry/Polyline",
            "esri/geometry/Polygon",
            "esri/geometry/Extent",
            "esri/geometry/geometryEngine",
            "esri/geometry/SpatialReference",

            //样式部分
            "esri/symbols/SimpleMarkerSymbol",
            "esri/symbols/PictureMarkerSymbol",
            "esri/symbols/SimpleLineSymbol",
            "esri/symbols/LineSymbol3DLayer",
            "esri/symbols/LineSymbol3D",
            //组件部分
            "esri/widgets/Legend",
            "esri/widgets/Expand",
            "esri/widgets/Print",
            "esri/widgets/Home",
            "esri/widgets/Sketch",
            "esri/widgets/Editor",
            "esri/widgets/Compass",
            "esri/widgets/Fullscreen",
            "esri/widgets/Swipe",
            //渲染部分
            "esri/renderers/ClassBreaksRenderer",
            //其他
            "esri/Graphic",
            "esri/geometry/support/webMercatorUtils",
            "esri/PopupTemplate",
            'esri/tasks/GeometryService',
            "esri/core/watchUtils",
            'esri/tasks/support/LinearUnit',
            'esri/tasks/support/FeatureSet',
            'esri/tasks/Geoprocessor',
            //自定义组件
            'esri/integration/LayersControl',
            'esri/integration/SwitchBaseMap',
            'esri/integration/GisTools',
            "dojo/domReady!"
        ], options).then(function([esriConfig, Basemap, Map, MapView, SceneView, TileLayer, GeoJSONLayer, MapImageLayer, FeatureLayer, GraphicsLayer,
            ClusterLayer, ArrowLine, WebTileLayer, Point, Polyline, Polygon, Extent, geometryEngine, SpatialReference, SimpleMarkerSymbol, PictureMarkerSymbol,
            SimpleLineSymbol, LineSymbol3DLayer, LineSymbol3D, Legend, Expand, Print, Home, Sketch, Editor, Compass, Fullscreen, Swipe, ClassBreaksRenderer, Graphic,
            webMercatorUtils, PopupTemplate, GeometryService, watchUtils, LinearUnit, FeatureSet, Geoprocessor, LayersControl, SwitchBaseMap, GisTools
        ]) {
            //字体库引用
            esriConfig.fontsUrl = arcgisConfig.getGisFont;
            //地图基础配置
            let appConfig = {
                    mapView: null,
                    sceneView: null,
                    activeView: null,
                    type: "2d",
                    container: args.container
                }
                //底图控制对象
            let basemap = new Basemap();
            //地图对象
            let map = new Map({
                basemap: basemap
            });
            // map.ground.opacity = 0.5;
            map.ground.surfaceColor = '#02253e';
            
            //初始化参数
            let initialViewParams = {
                zoom: 8,
                scale: 8850000,
                center: [101.2704722,37.79858333],
                container: appConfig.container,
                map: map
            };
            //二维视图
            appConfig.mapView = new MapView(initialViewParams);
            //去除logo
            appConfig.mapView.ui.remove(["zoom", "attribution"]);
            appConfig.activeView = appConfig.mapView;
            //三维视图
            initialViewParams.container = null;
            appConfig.sceneView = new SceneView(initialViewParams);
            appConfig.sceneView.ui.remove(["zoom", "attribution", "navigation-toggle", "compass"]);

            let switchbase = new SwitchBaseMap({
                basemap: basemap,
                url: arcgisConfig.yxdt
            })

            let home = new Home({
                view: appConfig.activeView
            })

            let swipe = new Swipe({
                view: appConfig.mapView,
                direction: "horizontal", //卷帘轴的方向，可以是水平或者垂直方向，此处为垂直方向
                position: 50 //卷帘轴初始位置，为%制，此处意思时初始时出现在中间位置
            });

            let sourceGraphicsLayer = new GraphicsLayer({
                id: 'sourceGraphicsLayer'
            })
            map.add(sourceGraphicsLayer);
            let sourceGraphicsLayer1 = new GraphicsLayer({
                id: 'sourceGraphicsLayer1'
            })
            map.add(sourceGraphicsLayer1);
            let cameraGraphicsLayer = new GraphicsLayer({
                id: 'sourceGraphicsLayer'
            })
            map.add(cameraGraphicsLayer);

            let yuntaiViewLayer = new GraphicsLayer({
                id: 'yuntaiViewLayer'
            })
            map.add(yuntaiViewLayer);

            
            let mapObj = {
                view: appConfig.activeView,
                map: map,
                LayersControl: LayersControl,
                appConfig: appConfig,
                switchbase: switchbase,
                GisTools: GisTools,
                FeatureLayer: FeatureLayer,
                Polyline: Polyline,
                ArrowLine: ArrowLine,
                home: home,
                Point: Point,
                Polygon: Polygon,
                Fullscreen: Fullscreen,
                geometryEngine: geometryEngine, //几何计算
                Graphic: Graphic,
                swipe: swipe,
                sourceGraphicsLayer: sourceGraphicsLayer, //添加资源图层
                sourceGraphicsLayer1: sourceGraphicsLayer1, //添加资源图层
                cameraGraphicsLayer: cameraGraphicsLayer, //生态科研历史监测图层
                yuntaiViewLayer: yuntaiViewLayer, //可视域分析显示图层
                LinearUnit: LinearUnit,
                FeatureSet: FeatureSet,
                Geoprocessor: Geoprocessor,
                Swipe:Swipe,
                TileLayer:TileLayer
            };
            /**
             * view 点击事件
             */
            clickView(mapObj);
            resolve(mapObj);
        }).catch((err) => {
            reject(err);
        });
    });
}